package com.chushouya.common.applet;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chushouya.common.util.RsaUtil;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Jsons;
import com.general.framework.core.lang.Validate;
import com.google.common.base.Objects;
import org.apache.commons.codec.binary.Base64;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

public abstract class JPushClient {

//    -----BEGIN PUBLIC KEY-----
//    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDaxZHZ5f9lEdtKeTb3wxogpe7v
//5AcOaQYx9kaPmE2g4TLEtpwMZaRoUz37EgwFGoGallri4eHu3ud3KBkEWHXH4SsM
//    XJ+4FY9Piksm5jMXQAvoxPVPAt1UpDEmFWZio83NpL/tRnrr29yBZsdaWCnDBjbt
//    Td/Ucin9VP+DZENkvQIDAQAB
//-----END PUBLIC KEY-----
//
//
//            -----BEGIN PRIVATE KEY-----
//    MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANrFkdnl/2UR20p5
//    NvfDGiCl7u/kBw5pBjH2Ro+YTaDhMsS2nAxlpGhTPfsSDAUagZqWWuLh4e7e53co
//    GQRYdcfhKwxcn7gVj0+KSybmMxdAC+jE9U8C3VSkMSYVZmKjzc2kv+1Geuvb3IFm
//    x1pYKcMGNu1N39RyKf1U/4NkQ2S9AgMBAAECgYEA1X2mNoEXebcyS/v8Y06W6bNk
//    d+AL7skYzZrrAuh7IPuDAqq9n+90wIYzXxceecCDJ6uAqBYM2IHYWdTcjPwSepwQ
//    Hpzu6PR86HUBvE1l76eFUSkqz1DepoiifXLOVX6/CxcWu5EKcmkT6mO5uoNedJDT
//    AtOvpuA+w+yNYdnskiECQQD/QCxM/8gESQkxZfLEgyskksLJrq2qA8g2gbznB3TA
//    D2QSOzT8WPRcw7zNUZRZEm7PuaeWUr1hAJQNr1dk/J5ZAkEA22n7XjbDppFqdPx+
//    qdiSFxcb1BalhvyC8trQI+2oCknrTIzJdF3Hbr1tBffGn99PUPg7cGERo78LNQf1
//    oC8VBQJAJBHAcN0QObLMbsOWS7ixZwiqJ+/cMJM6PqGUvRzrnZxPKthdA2/akCVE
//    z8rAaNnW+NmmWfK1OjjnswtdETM1eQJBAKRaj+sztHyZ4pd851hMnKnlKnhE+nrR
//    vTkzBMRU6hHC6xltQB4FfTULeZYF0ufAe4ZYWxXD8FWq7jcsHiZ9/g0CQQDdWcs/
//    MtCE0L/QjM5zZEmq/lNHIYIYljREPE60jWqBvoOyKGIJFgnQcKgd70XwUQ3861e+
//    M4KC4ERryMeJK67F
//-----END PRIVATE KEY-----

    private static final String LOGIN_TOKEN_URL = "https://api.verification.jpush.cn/v1/web/loginTokenVerify";
    private static final String APP_KEY = "6f9541e6f992d80408e35f8d";
    private static final String MASTER_SECRET = "a7f2587b69014641ba589fd8";
    private static final String PRI_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANrFkdnl/2UR20p5" +
            "NvfDGiCl7u/kBw5pBjH2Ro+YTaDhMsS2nAxlpGhTPfsSDAUagZqWWuLh4e7e53co" +
            "GQRYdcfhKwxcn7gVj0+KSybmMxdAC+jE9U8C3VSkMSYVZmKjzc2kv+1Geuvb3IFm" +
            "x1pYKcMGNu1N39RyKf1U/4NkQ2S9AgMBAAECgYEA1X2mNoEXebcyS/v8Y06W6bNk" +
            "d+AL7skYzZrrAuh7IPuDAqq9n+90wIYzXxceecCDJ6uAqBYM2IHYWdTcjPwSepwQ" +
            "Hpzu6PR86HUBvE1l76eFUSkqz1DepoiifXLOVX6/CxcWu5EKcmkT6mO5uoNedJDT" +
            "AtOvpuA+w+yNYdnskiECQQD/QCxM/8gESQkxZfLEgyskksLJrq2qA8g2gbznB3TA" +
            "D2QSOzT8WPRcw7zNUZRZEm7PuaeWUr1hAJQNr1dk/J5ZAkEA22n7XjbDppFqdPx+" +
            "qdiSFxcb1BalhvyC8trQI+2oCknrTIzJdF3Hbr1tBffGn99PUPg7cGERo78LNQf1" +
            "oC8VBQJAJBHAcN0QObLMbsOWS7ixZwiqJ+/cMJM6PqGUvRzrnZxPKthdA2/akCVE" +
            "z8rAaNnW+NmmWfK1OjjnswtdETM1eQJBAKRaj+sztHyZ4pd851hMnKnlKnhE+nrR" +
            "vTkzBMRU6hHC6xltQB4FfTULeZYF0ufAe4ZYWxXD8FWq7jcsHiZ9/g0CQQDdWcs/" +
            "MtCE0L/QjM5zZEmq/lNHIYIYljREPE60jWqBvoOyKGIJFgnQcKgd70XwUQ3861e+" +
            "M4KC4ERryMeJK67F";

    /**
     * 极光推送一键登录
     *
     * @param loginToken 认证 SDK 获取到的 loginToken
     * @param loginToken
     * @return 返回手机号码
     */
    public static String loginTokenVerify( String loginToken) {
        Validate.isBlank(loginToken, "loginToken不能为空");
        Map<String, String> map = new HashMap<>();
        map.put("loginToken", loginToken);
        try (HttpResponse response = HttpUtil.createPost(LOGIN_TOKEN_URL).basicAuth(APP_KEY, MASTER_SECRET).body(Jsons.toJsonString(map)).execute()) {
            Validate.isTrue(!response.isOk(), "一键登录状态码异常");
            JSONObject json = JSON.parseObject(response.body());
            Validate.isTrue(json.getIntValue("code") != 8000, json.getString("content"));
            try {
                return RsaUtil.decrypt(Base64.decodeBase64(json.getString("phone")), Base64.decodeBase64(PRI_KEY));
            } catch (Exception e) {
                throw Ex.business("手机号码解密异常", e);
            }
        }
    }

}
